/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.model.content.graph {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.dbg.view.content.graph.IMLDBGGraphModel;
	import com.googlecode.milib.dbg.view.content.graph.MLDBGGraphLogData;

	import flash.events.TimerEvent;
	import flash.system.System;
	import flash.utils.Timer;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGMemoryMonitorModel extends MLBroadcastClass implements IMLDBGGraphModel {

		public function get event_Updated():String {
			return 'Updated';
		}
		
		private var currentData:MLDBGGraphLogData;
		private var timer:Timer;		private var history:Array=[];

		public function MLDBGMemoryMonitorModel(updateDelay:uint=5000) {
			timer=new Timer(updateDelay);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			update();
		}
		
		public function getData():Array {
			return history;
		}
		
		public function getTextData():String {
			return 'total: '+Math.round(System.totalMemory/1048576)+'MB '+Math.round(System.totalMemory/1024)+'kB';
		}
		
		public function getMax():Number {
			return NaN;
		}
		
		public function getCurrentGraphLogData():MLDBGGraphLogData {
			return history[history.length-1];
		}

		private function update():void {
			currentData=new MLDBGGraphLogData(System.totalMemory);
			history.push(currentData);
			if(history.length>1000){
				history.shift();
			}
			broadcastEvent(event_Updated);
		}
		
		public function addLog(text:String):void {
			currentData.addLog(text);
		}
		
//********************************************************************************************
//		events for MLDBGMemoryMonitorModel
//********************************************************************************************
		protected function onTimer(event:TimerEvent):void {
			update();
		}
	}
}